package moni.anyou.com.view.view.team.adapter;


import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseActivity;
import moni.anyou.com.view.bean.response.base.ResTeamBean;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.view.LeaderDetailActivity;


/**
 * Created by Administrator on 2016/11/21.
 */

public class SelectTeamAdapter extends RecyclerView.Adapter<SelectTeamAdapter.MyViewHold> implements View.OnClickListener {

    private BaseActivity mContext;
    private LayoutInflater mInflater;
    private List<ResTeamBean.TeamBean> mItems=new ArrayList<>();

    public SelectTeamAdapter(BaseActivity context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext.mBaseActivity);
    }
    public SelectTeamAdapter(LeaderDetailActivity context, List<ResTeamBean.TeamBean> items) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext.mBaseActivity);
        mItems = items;
    }


    View mView;

    @Override
    public MyViewHold onCreateViewHolder(ViewGroup parent, int viewType) {

        MyViewHold holder = null;

        if (holder == null) {
            mView = mInflater.inflate(R.layout.activity_select_team_item, parent, false);
            holder = new MyViewHold(mView);
            mView.setTag(holder);
        } else {
            holder = (MyViewHold) mView.getTag();
        }
        mView.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHold mViewHold, final int position) {
        mViewHold.itemView.setTag(position);
        final ResTeamBean.TeamBean bean = mItems.get(position);

        mContext.setBitmaptoImageView(SysConfig.PicUrl+bean.teamPic,
                mViewHold.tv_item_pic,
                R.drawable.loading_null_21,
                R.drawable.loading_null_21,
                R.drawable.loading_err_21);
        mViewHold.tv_item_teamName.setText(bean.teamName);
        mViewHold.tv_item_routeName.setText(bean.routeName);
        mViewHold.tv_item_startTime.setText("时间:"+bean.startTime+"到"+bean.endTime);
        mViewHold.tv_item_member.setText("  成员("+bean.memberNumber+")  ");
        if (bean.status.equals("0")) {
            mViewHold.tv_item_member.setVisibility(View.GONE);
            mViewHold.tv_item_edit.setVisibility(View.VISIBLE);
            mViewHold.tv_item_del.setVisibility(View.VISIBLE);
            mViewHold.tv_item_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //mContext.marklike(position, getItem(position));
                    mEditClickListener.onItemClick(position, bean);
                }
            });
            mViewHold.tv_item_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //mContext.marklike(position, getItem(position));
                    mOnDeleteClickListener.onItemClick(position, bean);
                }
            });
        }
        else {
            mViewHold.tv_item_edit.setVisibility(View.GONE);
            mViewHold.tv_item_member.setVisibility(View.VISIBLE);
            mViewHold.tv_item_del.setVisibility(View.GONE);
            mViewHold.tv_item_member.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //mContext.marklike(position, getItem(position));
                    mViewMemberClickListener.onItemClick(position, bean);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mItems != null) {
            return mItems.size();
        }
        return 0;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (ResTeamBean.TeamBean) mItems.get((Integer) v.getTag()));
        }
    }

    class MyViewHold extends RecyclerView.ViewHolder {
        TextView tv_item_teamName;
        ImageView tv_item_pic;
        TextView tv_item_routeName;
        RelativeLayout rlteam;
        //TextView tv_item_leader;
        TextView tv_item_startTime;
        TextView tv_item_edit;
        TextView tv_item_del;
        TextView tv_item_member;
        public MyViewHold(View itemView) {
            super(itemView);
            tv_item_pic = (ImageView) itemView.findViewById(R.id.tv_item_pic);
            tv_item_teamName = (TextView) itemView.findViewById(R.id.tv_item_teamName);
            tv_item_routeName = (TextView) itemView.findViewById(R.id.tv_item_routeName);
            tv_item_startTime= (TextView) itemView.findViewById(R.id.tv_item_startTime);
            rlteam = (RelativeLayout) itemView.findViewById(R.id.rl_item_team);
            //tv_item_leader = (TextView) itemView.findViewById(R.id.tv_item_leader);
            tv_item_edit = (TextView) itemView.findViewById(R.id.tv_item_edit);
            tv_item_del = (TextView) itemView.findViewById(R.id.tv_item_del);
            tv_item_member = (TextView) itemView.findViewById(R.id.tv_item_member);
        }
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, ResTeamBean.TeamBean data);
    }

    public void setmOnItemClickListener(OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private OnEditClickListener mEditClickListener = null;
    private OnDeleteClickListener mOnDeleteClickListener = null;
    private ViewMemberClickListener mViewMemberClickListener = null;

    //define interface
    public static interface OnDeleteClickListener {
        void onItemClick(int position, ResTeamBean.TeamBean data);
    }

    //define interface
    public static interface OnEditClickListener {
        void onItemClick(int position,ResTeamBean.TeamBean data);
    }

    public static interface ViewMemberClickListener {
        void onItemClick(int position, ResTeamBean.TeamBean data);
    }

    public void setOnDeleteClickListener(OnDeleteClickListener mOnItemClickListener) {
        this.mOnDeleteClickListener = mOnItemClickListener;
    }

    public void setOnEditClickListener(OnEditClickListener mOnEditClickListener) {
        this.mEditClickListener = mOnEditClickListener;
    }

    public void setViewMemberClickListener(ViewMemberClickListener mViewMemberClickListener) {
        this.mViewMemberClickListener = mViewMemberClickListener;
    }


    public void addDatas(List<ResTeamBean.TeamBean>result) {

        if (mItems != null && result != null) {
            mItems.addAll(result);
        }
        notifyDataSetChanged();
    }
    public void setDatas(List<ResTeamBean.TeamBean>result) {
        if (result != null && mItems != null) {
            mItems.clear();
        }
        if (result != null) {
            mItems.addAll(result);
        }
        notifyDataSetChanged();
    }

}
